package com.example.mwavmedia.audio;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class PcmPlayer {
    private PlayThread playThread;
    public void startPlay(String path){
        stopPlay();
        playThread = new PlayThread(path,
                AudioManager.STREAM_MUSIC,
                44100,
                AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT,
                AudioTrack.MODE_STREAM);
        playThread.start();
    }
    public void stopPlay(){
        if (playThread == null) return;
        playThread.stopPlay();
        playThread = null;
    }
    private static class PlayThread extends Thread {
        private AudioTrack audioTrack;

        private FileInputStream inputStream;
        private final String path;
        private final int streamType;//音频流模式
        private final int sampleRate;
        private final int channelConfig;
        private final int audioFormat;
        private final int mod;//播放模式，一般使用流模式

        private int bufferSize;
        private boolean isStopPlay = false;

        public PlayThread(String path,int streamType,int sampleRate,
                          int channelConfig,int audioFormat,int mod){
            this.path = path;
            this.streamType = streamType;
            this.sampleRate = sampleRate;
            this.channelConfig = channelConfig;
            this.audioFormat = audioFormat;
            this.mod = mod;
        }

        @Override
        public void run() {
            super.run();
            initIO();
            initAudioTrack();
            play();
        }
        private void initIO(){
            if (TextUtils.isEmpty(path)) return;
            try {
               inputStream = new FileInputStream(path);
            }catch (IOException ioException){
                ioException.printStackTrace();
            }

        }
        private void initAudioTrack(){
            bufferSize = AudioTrack.getMinBufferSize(sampleRate,channelConfig,audioFormat);
            audioTrack = new AudioTrack(streamType,sampleRate,channelConfig,audioFormat,bufferSize,mod);
        }
        private void play(){
            if (audioTrack == null || inputStream == null) return;
            byte[] data = new byte[bufferSize];
            audioTrack.play();
            for (;;){
                if (isStopPlay){
                    break;
                }
                int readSize = -1;
                try {
                    readSize = inputStream.read(data);
                }catch (IOException ioException){
                    ioException.printStackTrace();
                }
                if (readSize <= 0){
                    isStopPlay = true;
                    continue;
                }
                audioTrack.write(data,0,readSize);
            }

        }
        void  stopPlay(){
            isStopPlay = true;
            try {
                join(2000);
            }catch (InterruptedException interruptedException){
                interruptedException.printStackTrace();
            }
        }
        private void  release(){
            if (audioTrack != null){
                audioTrack.stop();
                audioTrack.release();
                audioTrack = null;
            }
            if (inputStream != null){
                try {
                    inputStream.close();
                }catch (IOException exception){
                    exception.printStackTrace();
                }


            }
        }
    }
}
